import qiniu.config
import uuid
import random
from qiniu import Auth, put_file, etag

def qiniu_upload(file):
    access_key = 'jL6sbbAHGsPHaLEgtHX7uWMfhDwbhn-d4NOAI-bV'
    secret_key = 'InlXYxcYF3RY6iuHvhkye8JJ17pgdknaR0WlQE-f'
    binary_file = file.read()
    #构建鉴权对象
    q = Auth(access_key, secret_key)
    #要上传的空间
    bucket_name = 'mff-project'
    #上传后保存的文件名
    key = '%s.jpg'%str(uuid.uuid4()).split('-')[random.randint(1,4)]
    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    ret, info = qiniu.put_data(token, key, binary_file)
    return key